package com.database;

import com.entity.UserEntity;
import com.sun.org.apache.xpath.internal.SourceTree;

import javax.annotation.ManagedBean;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.transaction.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Logger;


@ManagedBean
@SessionScoped
public class DataBaseBean {
    @Resource
    private UserTransaction ut;
    @PersistenceContext(unitName = "UserPU")
    private EntityManager entityManager;

    private static final Logger logger = Logger.getLogger("com.database.DataBaseBean");


    public void add(UserEntity userEntity) throws Exception {
      // javax.persistence.PersistenceUnitUtil jpaUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
        System.out.println("Előtte " +entityManager.contains(userEntity));
        ut.begin();
        entityManager.persist(userEntity);
        System.out.println("persist után " +entityManager.contains(userEntity));
   //    entityManager.remove(userEntity);
    //    System.out.println("remove után " +entityManager.contains(userEntity));
      //  entityManager.persist(userEntity);
      //  System.out.println("persist után " +entityManager.contains(userEntity));
        entityManager.detach(userEntity);
         System.out.println("detach után " +entityManager.contains(userEntity));

       UserEntity newuserEntity= entityManager.merge(userEntity);
        System.out.println("detach után merge után " +entityManager.contains(newuserEntity));

        ut.commit();

    }

//    public void delete(UserEntity userEntity) throws Exception {
//        entityManager.remove(userEntity);
//    }

    ArrayList<UserEntity> getUsers() {
        ArrayList<UserEntity> result = (ArrayList<UserEntity>) entityManager.createQuery("from UserEntity").getResultList();
        return result;
    }


    public ArrayList<UserEntity> getCheckedUsers() {
        ArrayList<UserEntity> checkedUsers = new ArrayList<UserEntity>();
        for (UserEntity u : getUsers()) {
                if (u.getCheckbox() == 1) {
                    checkedUsers.add(u);
                }
            }
            return checkedUsers;
    }

    public boolean validate(String username, String password) {
        for (UserEntity u : getUsers()) {
            if (u.getUserName().equals(username) && u.getPassword().equals(password)) {
                return true;
            }
        }
        return false;
    }

    public void check(String username) throws Exception {
        for (UserEntity u : getUsers()) {
            if (u.getUserName().equals(username)) {
                u.setCheckbox(1);

                ut.begin();
                entityManager.merge(u);
                ut.commit();
            }
        }
    }
    public void unCheck(String username) throws Exception {
        for (UserEntity u : getUsers()) {
            if (u.getUserName().equals(username)) {
                u.setCheckbox(0);
                ut.begin();
                entityManager.merge(u);
                ut.commit();
            }
        }
    }


    public boolean isCheckedInit(String username, String password) {
        for (UserEntity u : getUsers()) {
            if (u.getUserName().equals(username)) {
                if (u.getCheckbox() == 0) {
                    return false;
                }

            }
        }
        return true;
    }


}
